home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH6 / SRC / LEASTSQ2.FRM < prev    next >
Text File  |  1996-03-29  |  5KB  |  194 lines

  1. VERSION 4.00
  2. Begin VB.Form LeastSquareForm 
  3.    Caption         =   "Quadratic Least Squares"
  4.    ClientHeight    =   5310
  5.    ClientLeft      =   2085
  6.    ClientTop       =   900
  7.    ClientWidth     =   4830
  8.    Height          =   6000
  9.    Left            =   2025
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   354
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   322
  14.    Top             =   270
  15.    Width           =   4950
  16.    Begin VB.CommandButton CmdGo 
  17.       Caption         =   "Go"
  18.       Default         =   -1  'True
  19.       Enabled         =   0   'False
  20.       Height          =   375
  21.       Left            =   2040
  22.       TabIndex        =   1
  23.       Top             =   4920
  24.       Width           =   615
  25.    End
  26.    Begin VB.PictureBox Canvas 
  27.       AutoRedraw      =   -1  'True
  28.       Height          =   4815
  29.       Left            =   0
  30.       ScaleHeight     =   317
  31.       ScaleMode       =   3  'Pixel
  32.       ScaleWidth      =   317
  33.       TabIndex        =   0
  34.       Top             =   0
  35.       Width           =   4815
  36.    End
  37.    Begin VB.Menu mnuFile 
  38.       Caption         =   "&File"
  39.       Begin VB.Menu mnuFileExit 
  40.          Caption         =   "E&xit"
  41.       End
  42.    End
  43. End
  44. Attribute VB_Name = "LeastSquareForm"
  45. Attribute VB_Creatable = False
  46. Attribute VB_Exposed = False
  47. Option Explicit
  48.  
  49. Dim NumPts As Integer
  50. Dim PtX() As Single
  51. Dim PtY() As Single
  52.  
  53. ' ************************************************
  54. ' Compute the a, b, and c values for the least
  55. ' squares quadratic.
  56. ' ************************************************
  57. Sub GetLeastSquaresValues(num As Integer, x() As Single, Y() As Single, avalue As Single, bvalue As Single, cvalue As Single)
  58. Dim A As Single
  59. Dim B As Single
  60. Dim C As Single
  61. Dim D As Single
  62. Dim E As Single
  63. Dim F As Single
  64. Dim G As Single
  65. Dim x2 As Single
  66. Dim x3 As Single
  67. Dim x4 As Single
  68. Dim C2BE As Single
  69. Dim E2CN As Single
  70. Dim BDAF As Single
  71. Dim CFBG As Single
  72. Dim ACB2 As Single
  73. Dim denom As Single
  74. Dim i As Integer
  75.  
  76.     ' Compute the sums.
  77.     For i = 1 To NumPts
  78.         x2 = PtX(i) * PtX(i)
  79.         x3 = x2 * PtX(i)
  80.         x4 = x2 * x2
  81.         A = A + x4
  82.         B = B + x3
  83.         C = C + x2
  84.         D = D + PtY(i) * x2
  85.         E = E + PtX(i)
  86.         F = F + PtY(i) * PtX(i)
  87.         G = G + PtY(i)
  88.     Next i
  89.     
  90.     ' Compute the quadratic parameters.
  91.     C2BE = C * C - B * E
  92.     E2CN = E * E - C * NumPts
  93.     BDAF = B * D - A * F
  94.     CFBG = C * F - B * G
  95.     ACB2 = A * C - B * B
  96.     denom = (B * C - A * E) * C2BE - _
  97.             (C * E - B * NumPts) * (B * B - A * C)
  98.     
  99.     avalue = _
  100.     ((C * D - B * F) * E2CN - (E * F - C * G) * C2BE) / _
  101.     (ACB2 * E2CN + C2BE * C2BE)
  102.     
  103.     bvalue = _
  104.     (CFBG * (B * C - A * E) - BDAF * (C * E - B * NumPts)) / _
  105.     denom
  106.  
  107.     cvalue = _
  108.     (BDAF * (C * C - B * E) + CFBG * ACB2) / _
  109.     denom
  110. End Sub
  111.  
  112.  
  113.  
  114.  
  115.  
  116. ' ************************************************
  117. ' Add this point to the list of points.
  118. ' ************************************************
  119. Private Sub Canvas_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
  120. Const GAP = 2
  121.  
  122.     ' If this is the first point, erase the screen.
  123.     If NumPts < 1 Then Canvas.Cls
  124.     
  125.     ' Record the new point.
  126.     NumPts = NumPts + 1
  127.     ReDim Preserve PtX(1 To NumPts)
  128.     ReDim Preserve PtY(1 To NumPts)
  129.     PtX(NumPts) = x
  130.     PtY(NumPts) = Y
  131.  
  132.     ' Display the point.
  133.     Canvas.Line (x - GAP, Y - GAP)-(x + GAP, Y + GAP), , BF
  134.  
  135.     ' If NumPts >= 2, enable the Go button.
  136.     If NumPts >= 2 Then CmdGo.Enabled = True
  137. End Sub
  138.  
  139.  
  140. ' ************************************************
  141. ' Draw the least squares fit curve.
  142. ' ************************************************
  143. Private Sub CmdGo_Click()
  144.     CmdGo.Enabled = False
  145.  
  146.     DrawCurve
  147.     
  148.     ' Prepare to get a new set of points.
  149.     NumPts = 0
  150. End Sub
  151. ' ************************************************
  152. ' Draw the least squares line.
  153. ' ************************************************
  154. Sub DrawCurve()
  155. Dim A As Single
  156. Dim B As Single
  157. Dim C As Single
  158. Dim x1 As Single
  159. Dim x2 As Single
  160. Dim i As Integer
  161. Dim x As Single
  162. Dim dx As Single
  163.  
  164.     ' Get the parameters for the quadratic.
  165.     GetLeastSquaresValues NumPts, PtX, PtY, A, B, C
  166.     
  167.     ' Find the minimum and maximum X values.
  168.     x1 = PtX(1) ' This will be the minimum X value.
  169.     x2 = x1     ' This will be the maximum X value.
  170.     For i = 2 To NumPts
  171.         If x1 > PtX(i) Then x1 = PtX(i)
  172.         If x2 < PtX(i) Then x2 = PtX(i)
  173.     Next i
  174.     
  175.     ' Draw the curve.
  176.     Canvas.CurrentX = x1
  177.     Canvas.CurrentY = A * x1 * x1 + B * x1 + C
  178.     
  179.     dx = (x2 - x1) / 100    ' Use 100 increments.
  180.     x = x1 + dx
  181.     Do While x < x2
  182.         Canvas.Line -(x, A * x * x + B * x + C)
  183.         x = x + dx
  184.     Loop
  185.     
  186.     Canvas.Line -(x2, A * x2 * x2 + B * x2 + C)
  187. End Sub
  188.  
  189. Private Sub mnuFileExit_Click()
  190.     Unload Me
  191. End Sub
  192.  
  193.  
  194.